
<template>
  <div class="header_information page-container">
    <!-- 头部 -->
    <el-row class="header_name">
      <el-col :span="24" class="header_text">
        <h1>赛项类型管理</h1>
      </el-col>
      <el-col :span="24">
        <div class="header_line"></div>
      </el-col>
    </el-row>
    <!-- 按钮 -->
    <el-row class="el_row_btn">
      <el-col :span="24" class="content">
        <div class="content_top_btn">
          <div>
            <el-button size="large" :icon="Plus" type="primary" @click="handleOpenEventedModal">新增赛项类型</el-button>

          </div>
          <!-- <div>
            <el-input
              class="top_search_inp"
              v-model="query_data.studentName"
              placeholder="请输入类型姓名"
            />
            <el-button
              size="large"
              :icon="Search"
              circle
              :loading="loading"
              @click="changeInp"
            />
          </div> -->
        </div>
      </el-col>
    </el-row>
    <!-- 内容 -->
    <el-row>
      <el-col :span="24">
        <el-table v-loading="loading" empty-text="暂无数据..." :data="tableData" border style="width: 100%">
          <el-table-column label="ID" prop="typeId" width="150" />
          <el-table-column prop="eventType" label="类型名称" />

          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-dropdown trigger="click" @command="(value) => handleOperate(value, scope.row)">
                <span class="el_dropdown_link">
                  操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item :icon="Edit" command="edit-info"> 编辑 </el-dropdown-item>
                    <el-dropdown-item :icon="Delete" command="delect-item"> 删除 </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>

          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 编辑信息 -->
    <el-dialog v-model="info_modal" title="编辑类型信息" class="info-modal" width="30%">
      <el-form :model="addOrcompile" ref="reg_data_ref" :rules="reg_data_rules">
        <el-form-item label="类型名称" :label-width="formLabelWidth" prop="eventType">
          <el-input v-model="addOrcompile.eventType" autocomplete="off" clearable placeholder="请输入类型名称" maxlength="15"
            show-word-limit />
        </el-form-item>

      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="info_modal = false">取消</el-button>
          <el-button type="primary" @click="handleeventTypeAdd" color="#1964d6">
            保存
          </el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 新增类型 -->
    <div class="pagination">
      <el-dialog v-model="dialogGroups" title="新增类型" class="info-modal" width="30%">
        <el-form :model="addOrcompile" ref="reg_data_ref" :rules="reg_data_rules">
          <el-form-item label="类型名称" :label-width="formLabelWidth" prop="eventType">
            <el-input clearable v-model="addOrcompile.eventType" autocomplete="off" placeholder="请输入类型名称" />
          </el-form-item>

        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogGroups = false">取消</el-button>
            <el-button type="primary" color="#1964d6" @click="handleAddgroup">
              保存
            </el-button>
          </span>
        </template>
      </el-dialog>
      <el-pagination background layout="prev, pager, next, total" :total="total" @current-change="handlePage" />
    </div>
  </div>
</template>

<script  setup>
import {
  Edit, Delete, Search, Plus
} from "@element-plus/icons-vue";
import { onMounted, ref, watch } from "vue";
import { usePage } from "@/hooks/usePage"

import { ElMessage, ElMessageBox } from 'element-plus';

import { eventTypeAdd, eventTypePage, eventTypeUpdate, eventTypeDelete } from "@/api/gradingTeacher";
// 禁止空格
const inputValueTrim = ''
const formLabelWidth = ref(100)
// 编辑
const addOrcompile = ref({
  typeId: '',
  eventType: ''
})

const handleeventTypeAdd = async () => {
  reg_data_ref.value.validate(async (v) => {
    if (!v) {
      return;
    }
    let res = await eventTypeUpdate(addOrcompile.value)

    if (res.success) {
      ElMessage.success(res.message);
      info_modal.value = false
      handleLoad(true);
    } else {
      ElMessage.error(res.message)
    }
    addOrcompile.value = {}
  });
}


const reg_data_ref = ref();
const reg_data_rules = ref({
  groupDescribe: [
    {
      type: 'string',
      required: true,
      message: '请输入类型名称',
      trigger: 'blur',
    },
    //不能全部输入空格
    {
      required: true,
      transform: (value) => value && value.trim(),
      message: '类型名称不能全部为空',
      trigger: 'blur'
    }
  ],
});


// 保存新增
const handleAddgroup = async () => {
  reg_data_ref.value.validate(async (v) => {
    if (!v) {
      return;
    }
    let res = await eventTypeAdd({ eventType: addOrcompile.value.eventType })

    if (res.success) {
      ElMessage.success(res.message);
      dialogGroups.value = false
      handleLoad(true);
    } else {
      ElMessage.error(res.message)
    }
  });
}


/**
 * 信息弹窗数据
 */
const info_modal = ref(false)

/**
 * 操作
 */
const handleOperate = (command, row) => {

  if (command == 'edit-info') {
    // 编辑
    info_modal.value = true;
    const find = tableData.value.find((item) => row.typeId == item.typeId)

    if (!find) {
      return
    }
    addOrcompile.value.eventType = find.eventType
    addOrcompile.value.typeId = find.typeId
  } else if (command == 'delect-item') {
    // 删除
    ElMessageBox.confirm('是否删除该类型', "提示", {
      "cancel-button-text": "取消",
      "confirm-button-text": "确定",
    }).then(async () => {
      let res = await eventTypeDelete(
        {
          typeId: row.typeId
        }
      )
      if (!res.success) {
        ElMessage.error(res.message);
      } else {
        handleLoad(true);
        ElMessage.success(res.message);
      }
    })
  }
}


// 新增类型
const dialogGroups = ref(false)
const handleOpenEventedModal = () => {
  dialogGroups.value = true
  addOrcompile.value = {}
}



const input = ref('')
const {
  total: total,
  data: tableData,
  query_data,
  loading,
  handleLoad,
  handlePage
} = usePage({
  api_fn: eventTypePage,
})

onMounted(() => {
  // handleGetAllStudent()
  handleLoad(true);
})
</script>

<style lang="scss" scoped>
.header_information {
  padding: 20px;

  .header_name {
    margin-left: 10px;
    margin-right: 10px;

    .header_text {
      margin-left: -10px;
      margin-right: -10px;
    }

    h1 {
      font-size: 28px;
    }

    // 线
    .header_line {
      display: block;
      height: 1px;
      width: 100%;
      margin: 24px 0 20px 0;
      background-color: #8473f717;
    }
  }

  //   内容
  :deep(.table_btn_success) {
    padding: 0 5px !important;
    border: none;
  }

  :deep(.el-table) .cell {
    padding: 0 10px;
  }

  .el_row_btn {
    margin-bottom: 20px;

    .content {
      .content_top_btn {
        display: flex;
        justify-content: space-between;

        .top_search_inp {
          width: 300px;
          height: 40px;
          margin-right: 5px;
        }

        .top_search_btn {
          width: 110px;
          margin-right: 5px;
        }
      }
    }
  }

  .el-radio {
    margin: 0 0 0 5px;
  }

  .el_dropdown_link {
    font-size: 14px;
  }

  :deep(.el-table)__cell {
    padding: 12px 0 !important;
  }

  // 分页
  .pagination {
    margin-top: 30px;
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50px;
  }
}
</style>
